package com.biShe.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadImg {
    //阿里域名
    public static final String ALi_DOMIN = "https://kakaxiiii.oss-cn-chengdu.aliyuncs.com/";

    public static String upLoadImage(MultipartFile file) throws IOException {
        //生成文件名
        String originalFilename = file.getOriginalFilename();//原来图片名
        String ext = "."+ FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + ext;
        // 地域结点
        String endpoint = "http://oss-cn-chengdu.aliyuncs.com";
        String accessKeyId = "LTAI5tKLRcREPycuPorCkjam";
        String accessKeySecret = "rZeckh9h8qvFSCtCNOtpNXzeIMx0uT";
        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        ossClient.putObject(
                "kakaxiiii",//仓库名
                fileName,//文件名
                file.getInputStream()
        );
    ossClient.shutdown();
        return ALi_DOMIN+fileName;
    }


}
